Sandbox vectors

Let’s define some vectors which can be used for demonstrations:

manyNumbers <- sample( 1:1000, 20 )
manyNumbers
 [1] 846 852 487 260 331 658 552 733 742 148 822 678 500  73 133 687 249 127 688 378
manyNumbersWithNA <- sample( c( NA, NA, NA, manyNumbers ) )
manyNumbersWithNA
 [1] 148 688 133 852  73 846 331  NA 260 687 249 378 678 487  NA 500 552  NA 127 658 733 742 822
duplicatedNumbers <- sample( 1:5, 10, replace = TRUE )
duplicatedNumbers
 [1] 4 4 1 2 1 1 1 3 1 1
letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
mixedLetters <- c( sample( letters, 5 ), sample( LETTERS, 5 ) )
mixedLetters
 [1] "l" "e" "v" "a" "y" "G" "E" "O" "K" "M"

Are all/any elements TRUE

  • Input: logical vector
  • Output: single logical value
  • Task: try, understand what happens when you use manyNumbersWithNA instead of manyNumbers.
all( manyNumbers <= 1000 )
[1] TRUE
all( manyNumbers <= 500 )
[1] FALSE
any( manyNumbers > 1000 )
[1] FALSE
any( manyNumbers > 500 )
[1] TRUE
all( !is.na( manyNumbers ) )
[1] TRUE
any( is.na( manyNumbers ) )
[1] FALSE

Which elements are TRUE

Input: logical vector Output: vector of numbers (positions)

which( manyNumbers > 900 )
integer(0)
which( manyNumbersWithNA > 900 )
integer(0)
which( is.na( manyNumbersWithNA ) )
[1]  8 15 18

Filtering vector elements

  • Input: any vector and filtering condition
  • Output: elements of the input vector
  • Note: several ways to get the same effect
manyNumbers[ manyNumbers > 900 ] # indexing by logical vector
integer(0)
manyNumbers[ which( manyNumbers > 900 ) ] # indexing by positions
integer(0)
somePositions <- which( manyNumbers > 900 )
manyNumbers[ somePositions ]
integer(0)

Are some elements among other elements

  • Input: two vectors
  • Output: a logical vector corresponding to the first input vector
"A" %in% LETTERS
[1] TRUE
c( "X", "Y", "Z" ) %in% LETTERS
[1] TRUE TRUE TRUE
all( c( "X", "Y", "Z" ) %in% LETTERS )
[1] TRUE
all( mixedLetters %in% LETTERS )
[1] FALSE
any( mixedLetters %in% LETTERS )
[1] TRUE
mixedLetters[ mixedLetters %in% LETTERS ]
[1] "G" "E" "O" "K" "M"
mixedLetters[ !( mixedLetters %in% LETTERS ) ]
[1] "l" "e" "v" "a" "y"
manyNumbers %in% 300:600
 [1] FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
[18] FALSE FALSE  TRUE
which( manyNumbers %in% 300:600 )
[1]  3  5  7 13 20
sum( manyNumbers %in% 300:600 )
[1] 5

Pick one of two (three) depending on condition

  • Input: a logical vector and two vectors additional vectors (for TRUE, for FALSE)
  • Output: elements of the additional vectors
  • Note: it can take care of NAs
if_else( manyNumbersWithNA >= 500, "large", "small" )
 [1] "small" "large" "small" "large" "small" "large" "small" NA      "small" "large" "small" "small" "large"
[14] "small" NA      "large" "large" NA      "small" "large" "large" "large" "large"
if_else( manyNumbersWithNA >= 500, "large", "small", "UNKNOWN" )
 [1] "small"   "large"   "small"   "large"   "small"   "large"   "small"   "UNKNOWN" "small"   "large"  
[11] "small"   "small"   "large"   "small"   "UNKNOWN" "large"   "large"   "UNKNOWN" "small"   "large"  
[21] "large"   "large"   "large"  
# here integer 0L is needed instead of real 0.0 
# manyNumbersWithNA contains integer numbers and the method complains
if_else( manyNumbersWithNA >= 500, manyNumbersWithNA, 0L ) 
 [1]   0 688   0 852   0 846   0  NA   0 687   0   0 678   0  NA 500 552  NA   0 658 733 742 822

Duplicates and unique elements

  • Input: a vector
unique( duplicatedNumbers )
[1] 4 1 2 3
unique( c( NA, duplicatedNumbers, NA ) )
[1] NA  4  1  2  3
duplicated( duplicatedNumbers )
 [1] FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE

Positions of max/min elements

which.max( manyNumbersWithNA )
[1] 4
manyNumbersWithNA[ which.max( manyNumbersWithNA ) ]
[1] 852
which.min( manyNumbersWithNA )
[1] 5
manyNumbersWithNA[ which.min( manyNumbersWithNA ) ]
[1] 73
range( manyNumbersWithNA, na.rm = TRUE )
[1]  73 852

Sorting/ordering of vectors

manyNumbersWithNA
 [1] 148 688 133 852  73 846 331  NA 260 687 249 378 678 487  NA 500 552  NA 127 658 733 742 822
sort( manyNumbersWithNA )
 [1]  73 127 133 148 249 260 331 378 487 500 552 658 678 687 688 733 742 822 846 852
sort( manyNumbersWithNA, na.last = TRUE )
 [1]  73 127 133 148 249 260 331 378 487 500 552 658 678 687 688 733 742 822 846 852  NA  NA  NA
sort( manyNumbersWithNA, na.last = TRUE, decreasing = TRUE )
 [1] 852 846 822 742 733 688 687 678 658 552 500 487 378 331 260 249 148 133 127  73  NA  NA  NA
manyNumbersWithNA[1:5]
[1] 148 688 133 852  73
order( manyNumbersWithNA[1:5] )
[1] 5 3 1 2 4
rank( manyNumbersWithNA[1:5] )
[1] 3 4 2 5 1
sort( mixedLetters )
 [1] "a" "e" "E" "G" "K" "l" "M" "O" "v" "y"

Ranking of vectors

manyDuplicates <- sample( 10:15, 10, replace = TRUE )
rank( manyDuplicates )
 [1] 9.0 6.5 9.0 2.5 1.0 6.5 9.0 4.5 2.5 4.5
rank( manyDuplicates, ties.method = "min" )
 [1] 8 6 8 2 1 6 8 4 2 4
rank( manyDuplicates, ties.method = "random" )
 [1] 10  6  9  2  1  7  8  5  3  4

Rounding numbers

v <- c( -1, -0.5, 0, 0.5, 1, rnorm( 10 ) )
v
 [1] -1.0000000 -0.5000000  0.0000000  0.5000000  1.0000000 -0.9843264 -0.7281775 -0.1550162 -0.2553143
[10] -1.0911730 -0.3469435 -1.6596504  0.1390499  0.8675331  0.7196380
round( v, 0 )
 [1] -1  0  0  0  1 -1 -1  0  0 -1  0 -2  0  1  1
round( v, 1 )
 [1] -1.0 -0.5  0.0  0.5  1.0 -1.0 -0.7 -0.2 -0.3 -1.1 -0.3 -1.7  0.1  0.9  0.7
round( v, 2 )
 [1] -1.00 -0.50  0.00  0.50  1.00 -0.98 -0.73 -0.16 -0.26 -1.09 -0.35 -1.66  0.14  0.87  0.72
floor( v )
 [1] -1 -1  0  0  1 -1 -1 -1 -1 -2 -1 -2  0  0  0
ceiling( v )
 [1] -1  0  0  1  1  0  0  0  0 -1  0 -1  1  1  1

Naming vector elements

heights <- c( Amy = 166, Eve = 170, Bob = 177 )
heights
Amy Eve Bob 
166 170 177 
names( heights )
[1] "Amy" "Eve" "Bob"
names( heights ) <- c( "AMY", "EVE", "BOB" )
heights
AMY EVE BOB 
166 170 177 
heights[[ "EVE" ]]
[1] 170

Generating grids

expand_grid( x = c( 1:3, NA ), y = c( "a", "b" ) )
# A tibble: 8 x 2
      x y    
  <int> <chr>
1     1 a    
2     1 b    
3     2 a    
4     2 b    
5     3 a    
6     3 b    
7    NA a    
8    NA b    

Generating combinations

combn( c( "a", "b", "c", "d", "e" ), m = 2, simplify = TRUE )
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a"  "a"  "a"  "a"  "b"  "b"  "b"  "c"  "c"  "d"  
[2,] "b"  "c"  "d"  "e"  "c"  "d"  "e"  "d"  "e"  "e"  
combn( c( "a", "b", "c", "d", "e" ), m = 3, simplify = TRUE )
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a"  "a"  "a"  "a"  "a"  "a"  "b"  "b"  "b"  "c"  
[2,] "b"  "b"  "b"  "c"  "c"  "d"  "c"  "c"  "d"  "d"  
[3,] "c"  "d"  "e"  "d"  "e"  "e"  "d"  "e"  "e"  "e"  


Copyright © 2021 Biomedical Data Sciences (BDS) | LUMC